/**
 * 演示页面mixin
 */
import Tools from '@/tools/config.js'

module.exports = {
	data() {
		return {

		}
	},
	onShareAppMessage(res) {
		let a,type = 'mp',id = false;
		if(res.target != undefined){
			a = res.target.dataset
			type = a.type
			id = a.id
		}else{
			const page = getCurrentPages()[getCurrentPages().length - 1]
			if(page.route == "circlePages/businessCard"){
				type = 'mp'
				id = page.options.id
			}else if(page.route == "circlePages/details"){
				type = 'dongtai' // 动态详情分享
				id = page.options.iid
			}else if(page.route == "circlePages/group"){
				type = 'circle'
				id = page.options.id
			}
			console.log("11111", page,id)
		}
		if(!id && type == 'mp'){
			// 说明是自己
			id = uni.getStorageSync('openid')
		}
		if (type == 'dongtai') {
			// 商品分享
			return {
				title: "动态详情",
				path: '/circlePages/details?iid=' + id,
				success: (res) => {
					// uni.showToast({
					// 	title: '分享成功'
					// })
				},
				fail(res) {
					// uni.showToast({
					// 	title: '分享失败',
					// 	icon: 'none'
					// })
				},
				complate: () => {
					this.shareReuest(id)
				}
			}
		} else if (type == 'mp') {
			return {
				title: "名片",
				path: '/circlePages/businessCard?id=' + id,
				success: (res) => {
					// uni.showToast({
					// 	title: '分享成功'
					// })
				},
				fail(res) {
					// uni.showToast({
					// 	title: '分享失败',
					// 	icon: 'none'
					// })
				},
			}
		}else if(type == 'circle'){
			return {
				title: "圈子详情",
				path: '/circlePages/group?id=' + id,
				success: (res) => {
					// uni.showToast({
					// 	title: '分享成功'
					// })
				},
				fail(res) {
					// uni.showToast({
					// 	title: '分享失败',
					// 	icon: 'none'
					// })
				},
			}
		}
	},
	onLoad() {
		// 更新顶部导航栏信息
		this.updateCustomBarInfo()
	},
	methods: {
		getislogin(key) {
			let that = this
			return new Promise((resolve,reject)=>{
				uni.getStorage({
					key: key,
					success: function(res) {
						console.log('success')
						that.theloginxx = res.data
						if (that.theloginxx.length <= 0) {
							uni.showModal({
								title: '登录授权',
								content: "是否授权",
								success: async () => {
									await that.logintest()
									resolve()
								}
							})
						}else{
							resolve()
						}
					},
					fail: function(res) {
						uni.showModal({
							title: '登录授权',
							content: "是否授权",
							success: async () => {
								await that.logintest()
								resolve()
							}
						})
					}
				})
			})
		},
		logintest() {
			return new Promise((resolve,reject)=>{
				uni.getUserProfile({
					desc: "获取用户基本资料",
					lang: 'zh_CN',
					success: (user) => {
						//获取成功基本资料后开启登录，因为基本资料首先要授权
						uni.login({
							provider: 'weixin',
							success: function(code_res) {
								if (code_res.errMsg == "login:ok") {
									uni.showLoading()
									let code = code_res.code;
									console.log(code)
									uni.request({
										url: Tools.host + '/wshly.ashx',
										method: 'POST',
										data: {
											'lx': 'userlogin',
											'wxcode': code,
											'tximg': user.userInfo.avatarUrl,
											'nc': user.userInfo.nickName
										},
										success: (session_res) => {
											console.log('session_res：' + session_res
												.data.data)
											var thejson = JSON.parse(session_res
												.data.data);
											//成功
											uni.setStorageSync("openid", thejson
												.openid)
											uni.setStorageSync("unionid", thejson
												.unionid)
										},
										fail: (res) => {
											console.log('登录失败' + res)
											uni.showModal({
												title: '登录失败，请稍后重试',
												showCancel: false
											})
										},
										complete:()=>{
											resolve()
											uni.hideLoading()
										}
									});
								} else {
									uni.showModal({
										title: '登录失败，请稍后重试',
										showCancel: false
									})
									reject()
									uni.hideLoading()
								}
							},
							fail: (res) => {
								uni.showModal({
									title: '登录失败，请稍后重试',
									showCancel: false
								})
								reject()
								uni.hideLoading()
							}
						});
					},
					fail: (res) => {
						uni.showModal({
							title: "用户拒绝授权",
							showCancel: false
						})
						reject()
						uni.hideLoading()
					}
				});
			})
		},
		// 分享
		shareReuest(id) {
			let theopenid = uni.getStorageSync('openid')
			let theunionid = uni.getStorageSync('unionid')
			uni.request({
				url: Tools.host + '/wshly.ashx',
				method: 'POST',
				data: {
					'lx': 'dzfxgz',
					'openid': theopenid,
					'unionid': theunionid,
					'iid': id,
					'type': 2
				},
				success: (res) => {
					//成功
					if (res.data.return_code == '1') {
						uni.showToast({
							title: '分享成功',
							icon: "success"
						})
					}
				},
				fail: (res) => {
					console.log('失败' + res)
					uni.showModal({
						title: '失败，请稍后重试',
						showCancel: false
					})
				}
			});
		},
		// 点赞
		dianzan(item) {
			let theopenid = uni.getStorageSync('openid')
			let theunionid = uni.getStorageSync('unionid')
			uni.request({
				url: Tools.host + '/wshly.ashx',
				method: 'POST',
				data: {
					'lx': 'dzfxgz',
					'openid': theopenid,
					'unionid': theunionid,
					'iid': item.iid,
					'type': 1
				},
				success: (res) => {
					//成功
					if (res.data.return_code == '1') {
						item.likeCount++
						uni.showToast({
							title: '点赞成功',
							icon: "success"
						})
					}
				},
				fail: (res) => {
					console.log('失败' + res)
					uni.showModal({
						title: '失败，请稍后重试',
						showCancel: false
					})
				}
			});
		},
		goHome(){
			uni.removeStorageSync('circleHomeIndex')
			uni.reLaunch({
				url: '/pages/index?index=0'
			})
		},
		// 点击左上角返回按钮时触发事件
		goBack() {
			// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
			const pages = getCurrentPages()
			console.log("1111",pages)
			if (pages && pages.length > 0) {
				const firstPage = pages[0]
				if (pages.length == 1) {
					uni.reLaunch({
						url: '/pages/index?index=0'
					})
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			} else {
				uni.reLaunch({
					url: '/pages/index?index=0'
				})
			}
		},
		// 更新顶部导航栏信息
		async updateCustomBarInfo() {
			// 获取vuex中的自定义顶栏的高度
			let customBarHeight = this.vuex_custom_bar_height
			let statusBarHeight = this.vuex_status_bar_height
			// 如果获取失败则重新获取
			if (!customBarHeight) {
				try {
					const navBarInfo = await this.$t.updateCustomBar()
					customBarHeight = navBarInfo.customBarHeight
					statusBarHeight = navBarInfo.statusBarHeight
				} catch (e) {
					setTimeout(() => {
						this.updateCustomBarInfo()
					}, 10)
					return
				}
			}

			// 更新vuex中的导航栏信息
			this.$t.vuex('vuex_status_bar_height', statusBarHeight)
			this.$t.vuex('vuex_custom_bar_height', customBarHeight)
		},
		// 退出登陆
		layoutLogin(){
			uni.showModal({
				title:"提示",
				content:"确定退出登陆吗?",
				success:(res)=>{
					if(res.confirm){
						uni.clearStorageSync()
						uni.reLaunch({
							url:"/pages/index"
						})
					}
				}
			})
		},
		previewImgs(arr, prop, index){
			let a = arr.map(i=>i[prop])
			this.previewImg(a,index)
		},
		previewImg(url,current = 0) {
			console.log("触发了:",url)
			if(url){
				if(Array.isArray(url)){
					uni.previewImage({
						current: current,
						urls: url
					})
				}else{
					uni.previewImage({
						urls: [url]
					})
				}
			}
		}
	}
}